<?
	/* Se asegura que el archivo es incluido por el index */
		defined( 'LOAD_MODULE_ACTION' ) or die( 'No esta permitido el acceso directo a ' . __FILE__ );
	/* Cuando se hace el include del archivo automaticamente crea un objeto y lo guarda en una variable temporal */
		$this->oTmpObject = new Evento_contacto_lst( $this );

	class Evento_contacto_lst extends Lst
	{
	function Evento_contacto_lst( &$oModule )
		{
			$this->Lst( $oModule );
			$this->oFecha = new DateObject();
			$this->oModAdministracion =& loadModule( 'administracion' );
			$this->oPaging->setDefaultOrderBy('ncContacto');
			$this->aFields['cKeyLookUp'] = getParam( PARAM_KEYLOOKUP );
		}
	function setLevel()
		{
			switch ( $this->getOptionView() )
			{
				case 'L':
					$this->aShowColumns = array( 'Evento', 'Organizacion', 'Nombre','ncRotulo' );
					break;
			}
			if ( array_key_exists( 'ncEvento' , $this->aFields )  ) unset($this->aShowColumns[array_search( 'Evento',$this->aShowColumns)]);
		}
	function initialize()
		{
			$this->oFiltro = $this->oModule->loadClass( 'Evento_contacto_frm' );
			$this->aFields = array_merge ( (array)$this->aFields,  (array)$this->oFiltro->getFieldArray() );
			
			$this->aFields['ncUsuario'] = getParam( 'ncUsuario' );
			$this->setTitle ( "Eventos - Contactos" );
			$this->setSummary( "Esta tabla contiene los contactos de los Eventos" );
			if ( $this->aFields['ncEvento'] )
			{
				$cdEvento = $this->oDatabase->getData("SELECT cdEvento FROM gtEvento WHERE ncEvento = " . $this->aFields['ncEvento'] );
				$this->setTitle ( "Contactos de $cdEvento" );
				if ( $this->getOptionView() != 'C' )
				{
					$oTab =& $this->setIndex( INDEX_USER, TAB_TOP , 'ncActividad');
					$cSql = "SELECT ncActividad, cdActividad FROM acActividad WHERE ncActividad IN ( SELECT DISTINCT ncActividad FROM gtEventoContacto WHERE ncEvento = " . $this->aFields['ncEvento']  . " ) ORDER BY cdActividad";
					$oTab->setMatrix( $this->oModule->oDatabase->getMatrix($cSql) );
					$oTab =& $this->setIndex( INDEX_USER, TAB_BOTTOM , 'ncUsuario');
					$cSql = "SELECT ncUsuario, cUsuario FROM stUsuario WHERE ncUsuario IN ( SELECT DISTINCT ncUsuario FROM gtEventoContacto WHERE ncEvento = " . $this->aFields['ncEvento']  . " ) ";
					$oTab->setMatrix( $this->oModule->oDatabase->getMatrix($cSql) );
				}
			} else
			{
				if ( $this->getOptionView() != 'C' )
				{
					$oTab =& $this->setIndex( INDEX_USER, TAB_TOP , 'ncEvento');
					$cSql = "SELECT ncEvento, cSigla FROM gtEvento WHERE cSigla != '' ORDER BY dDesde DESC";
					$oTab->setMatrix( $this->oModule->oDatabase->getMatrix($cSql, 0, 10) );
				}
			}
			$this->addColumn ( 'Evento' );
			$this->addColumn ( 'Organizacion', 'cdOrganizacion' );
			$this->addColumn( 'Nombre', 'cNombreCompleto' );
			$this->addColumnTag( 'ncRotulo', ACTUALIZAR_TAG_CONTACTO, 'ncEvento,ncContacto', 'gtContactoRotulo', $this->oDatabase->getKeyArray('SELECT ncRotulo, cdRotulo FROM gtRotuloContacto' ) );
			$this->addColumn ( 'Telefono' );
			$this->addColumn ( 'Actividad', 'cdActividad' );
			$this->addColumn ( 'TelefonoOrganizacion' );
			$this->addColumn ( 'Ubicacion', 'cdUbicacion' );
			$this->addColumnLink ( 'Web' , 'cPaginaWeb');
			$this->addColumn ( 'Cargo' , 'cCargo' );
			$this->addColumn ( 'EmailOrganizacion' , 'cEmailOrganizacion' );
			$this->addColumn ( 'EmailLaboral' );
			$this->addColumn ( 'EmailPersonal' );
			$this->setColumnAction ( 'Evento', VER_EVENTO, 'ncEvento' );
			$this->setColumnAction ( 'Nombre', VER_PERSONA, 'ncPersona', '', &$this->oModAdministracion );
			$this->setColumnAction ( 'Organizacion', VER_ORGANIZACION, 'ncOrganizacion', '', &$this->oModAdministracion );
		}
	function makeColumn ( $aRow, $cField )
	{
		switch( $cField )
		{
			case 'Evento':
				$cReturn = $aRow['cdEvento'] ;
				if ( $aRow['cSigla'] ) $cReturn .= " ( " . $aRow['cSigla']  . " )";
				break;
			case 'EmailLaboral':
					if ( $aRow['ncPersona'] && $aRow['ncOrganizacion'] )
					{
						$cSql = "SELECT cEmail  FROM atPersonaEmail  WHERE ncPersona = "  . $aRow['ncPersona'] . " AND ncOrganizacion =  " . $aRow['ncOrganizacion'] ;
						$cReturn =  $this->oDatabase->getData($cSql);
					}
					break;
			case 'EmailPersonal':
					if ( $aRow['ncPersona'] )
					{
						$cSql = "SELECT cEmail  FROM atPersonaEmail  WHERE ncPersona = "  . $aRow['ncPersona'] . " AND ncOrganizacion is NULL";
						$cReturn =  $this->oDatabase->getData($cSql);
					}
					break;
			case 'TelefonoOrganizacion':
					if ( $aRow['ncOrganizacion'] )
					{
						$cSql = "SELECT Dom.cPrefijoTelefono, cTelefono, cdUbicacion  FROM atOrganizacionDomicilio as ODo JOIN atDomicilio  as Dom ON Dom.ncDomicilio = ODo.ncDomicilio  JOIN acUbicacion as Ubi ON Dom.ncUbicacion = Ubi.ncUbicacion AND Dom.ncPais = Ubi.ncPais WHERE ncOrganizacion = " . $aRow['ncOrganizacion'] . "  ORDER BY bPorOmision " ;
						$aData = $this->oDatabase->getArray($cSql);
						$aRow['TelefonoOrganizacion'] = '(' . $aData['cPrefijoTelefono'] . ') ' . $aData['cTelefono'] ;
						$aRow['cdUbicacion'] = $aData['cdUbicacion'] ;
						$cReturn = $aRow['TelefonoOrganizacion']  ;
					}
				break;
			case 'Telefono':
					if ( $aRow['ncPersona'] &&  $aRow['ncOrganizacion'] )
					{
						$cSql = "SELECT cCargo,  cDirecto FROM atOrganizacionPersona  WHERE ncOrganizacion = " . $aRow['ncOrganizacion'] . " AND ncPersona = "  . $aRow['ncPersona'] ;
						$aData = $this->oDatabase->getArray($cSql);
						$aRow['cCargo'] = $aData['cCargo'] ;
						$aRow['cDirecto'] = $aData['cDirecto'] ;
						$cReturn = ( $aRow['cDirecto'] )  ? $aData['cDirecto'] : $aDomicilio['cTelefono'] ;
					}
					break;
				case 'KeyLookUp':
					$cReturn = $aRow['ncEvento'] . "|" . $aRow['ncContacto'];
					break;
				case 'DescriptionLookUp':
					$cReturn = $aRow['cdOrganizacion'] . ' - ' . $aRow['cNombre'] . ' ' . $aRow['cApellido'];
					break;
			default:
					return false;
				break;
		}
		return $cReturn;
	}
	function query()
		{
			// Arma el Query
				$cSql = "SELECT EPe.ncContacto, EPe.ncActividad, Act.cdActividad, Eve.ncEvento, Eve.cdEvento, Eve.cSigla, Per.ncPersona, " . $this->oDatabase->dbConcat( 'Per.cApellido', "', '" , 'Per.cNombre') . " as cNombreCompleto, Org.ncOrganizacion, Org.cdOrganizacion, Org.cPaginaWeb, Org.cEmail as cEmailOrganizacion
							FROM gtEvento as Eve
							JOIN gtEventoContacto as EPe
								ON EPe.ncEvento = Eve.ncEvento
							LEFT JOIN atPersona as Per
								ON Per.ncPersona = EPe.ncPersona
							LEFT JOIN acActividad as Act
								ON Act.ncActividad = EPe.ncActividad
							LEFT JOIN atOrganizacion as Org
								ON Org.ncOrganizacion = EPe.ncOrganizacion";
			// Arma el Filtro de la Consulta
				$this->oWhere->addArray ( $this->oFiltro->getFilterdArray('EPe') );
				return $cSql . $this->oWhere->make();
		}
	function makeListActions()
		{
			$this->oModule->setBack();
			$cParametroRegistro =  $this->getFieldString();
			if ( $this->aFields['ncEvento'] )
			{
				$this->addListAction ( AGREGAR_UN_CONTACTO_A_UN_EVENTO , $cParametroRegistro . '&amp;bAltaSimplificada=1', 'I');
				$this->addListAction ( AGREGAR_UN_CONTACTO_A_UN_EVENTO , $cParametroRegistro . '&amp;bAltaSimplificada=0');
				$this->addListAction ( IMPORTAR_CONTACTOS, $cParametroRegistro, 'I', VENTANA_SECUNDARIA );
		}
		}
	function makeRowActions( $aRow )
		{
			$cParametroRegistro =  "ncEvento=" . $aRow["ncEvento"] . "&amp;ncContacto=" . $aRow["ncContacto"];
			$this->addRowAction ( MODIFICAR_UN_CONTACTO_DE_UN_EVENTO , $cParametroRegistro , '', VENTANA_SECUNDARIA);
			$this->addRowAction ( ELIMINAR_UN_CONTACTO_DE_UN_EVENTO , $cParametroRegistro);
		}
	}
?>